﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MLM.Data;

namespace MLM.AdminCP.Pages
{
    public partial class ConvertData : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            MLMDataContext ct = new MLMDataContext();
            var bills = ct.Bills;
            foreach (var bill in bills)
            {
                var billDetails = ct.BillDetails.Where(it => it.BillID == bill.BillID);
                var invoice = new Invoice
                                  {
                                      CreatedBy = bill.CreatedBy,
                                      DateTimeCreated = bill.DateTimeCreated,
                                      DepoID = bill.DepoID,
                                      IsManual = true,
                                      Status = bill.Status,
                                      UserID = bill.UserID
                                  };
                                
                foreach (var billDetail in billDetails)
                {
                    invoice.InvoiceDetails.Add(new InvoiceDetail
                                                    {                
                                                        Point = billDetail.Point,
                                                        Price = billDetail.Price,
                                                        PriceWithoutTax = billDetail.Price,
                                                        ProductBaseID = billDetail.ProductBaseID,
                                                        VAT = billDetail.VAT,
                                                        Quantity = billDetail.Quantity
                                                    });
                }
                ct.Invoices.AddObject(invoice);
            }            
            ct.SaveChanges();
        }
    }
}